from django.db import models
from django.utils import timezone
from django.conf import settings
from django.contrib.auth.models import User


# Create your models here.

# 角色表
# 资源表
# 角色资源表
# 用户基本表
# HR信息表
# 求职者信息表
# 第三方登录记录表
class Role(models.Model):
    # 角色名称：hr、普通求职、vip求职者
    name = models.CharField(max_length=100, verbose_name='角色名称')

    def __str__(self):
        return self.name

    class Meta:
        db_table = "role"


# 资源表
class Resource(models.Model):
    name = models.CharField(max_length=100, verbose_name='资源名称')
    url = models.CharField(max_length=500, verbose_name='资源路径')
    parent = models.ForeignKey('self', null=True, blank=True, verbose_name='父级资源id', on_delete=models.CASCADE,
                               related_name='children')

    def __str__(self):
        return self.name

    class Meta:
        db_table = "resource"


# 角色资源中间表
class Role_Resource(models.Model):
    role = models.ForeignKey(Role, verbose_name='角色id', on_delete=models.CASCADE)
    resource = models.ForeignKey(Resource, verbose_name='角色id', on_delete=models.CASCADE)

    class Meta:
        db_table = "role_resource"


# 用户基本信息表
class User(models.Model):
    phone = models.CharField(max_length=20, verbose_name='手机号', unique=True)
    username = models.CharField(max_length=100, verbose_name='用户名', null=True, blank=True, default='')
    email = models.EmailField(verbose_name='邮箱', null=True, blank=True, default='')
    password_hash = models.CharField(max_length=255, verbose_name='密码哈希', null=True, blank=True, default='')
    status = models.BooleanField(verbose_name='状态', default=False)
    role = models.ForeignKey(Role, verbose_name='角色id', on_delete=models.CASCADE)

    class Meta:
        db_table = "user"
        verbose_name = "用户"
        verbose_name_plural = "用户"

    def __str__(self):
        return self.username or self.phone


# 城市表
class City(models.Model):
    name = models.CharField(max_length=100, verbose_name='城市名称')
    pid = models.ForeignKey('self', null=True, blank=True, verbose_name='父级城市id', on_delete=models.CASCADE,
                            related_name='children')

    class Meta:
        db_table = 'city'


# 求职者浏览的公司


# 公司表
class Company(models.Model):
    name = models.CharField(max_length=100, verbose_name='公司名称')
    logo = models.CharField(max_length=500, verbose_name='公司logo')
    job_number = models.IntegerField(default=100, verbose_name='招聘职位数量')
    boss_number = models.IntegerField(default=10, verbose_name="boss数量")
    is_listed = models.IntegerField(
        choices=((0, "未融资"), (1, "天使轮"), (2, "A轮"), (3, "B轮"), (4, "C轮"), (5, "D轮及以上"), (6, "以上市"),
                 (7, "不需要融资")))
    employee_number = models.IntegerField(default=1000, verbose_name='员工数量')
    description = models.TextField(default="", verbose_name='公司简介')
    location = models.CharField(default="", max_length=100, verbose_name='公司地址')
    collect_number = models.IntegerField(default=0, verbose_name='被收藏数量')
    browse_number = models.IntegerField(default=0, verbose_name='被浏览数量')

    city = models.ForeignKey(
        City, verbose_name='城市id', on_delete=models.CASCADE,
        # related_name='company'
    )

    def __str__(self):
        return self.name

    class Meta:
        db_table = "company"


class HRInfo(models.Model):
    name = models.CharField(max_length=20, verbose_name='用户名', default='暂知', unique=True, null=True, blank=True)
    sex = models.IntegerField(verbose_name='性别', choices=((1, '女'), (2, '男')), null=True, blank=True)
    description = models.TextField(verbose_name='简介', null=True, blank=True)
    aveter = models.CharField(max_length=500, verbose_name='头像', default='暂知', null=True, blank=True)
    department = models.CharField(max_length=20, verbose_name='部门', default="暂知", null=True, blank=True)
    position = models.CharField(max_length=20, verbose_name='职位', default='暂知', null=True, blank=True)
    work_number = models.CharField(max_length=20, verbose_name='工号', null=True, blank=True)
    user = models.ForeignKey(User, verbose_name='用户id', on_delete=models.CASCADE)
    company = models.ForeignKey(Company, verbose_name='公司id', on_delete=models.CASCADE, null=True, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "hrinfo"


# 求职者信息表
class JobInfo(models.Model):
    name = models.CharField(max_length=20, verbose_name='用户名', unique=True, null=True, blank=True)
    sex = models.IntegerField(verbose_name='性别', choices=((1, '女'), (2, '男')), null=True, blank=True)
    birthdate = models.DateField(verbose_name='出生日期', null=True, blank=True)
    description = models.TextField(verbose_name='简介/自我介绍', null=True, blank=True)
    education = models.IntegerField(verbose_name='学历', choices=(
        (1, '初中以下'), (2, '初中'), (3, '中专'), (4, '高中'), (5, '大专'), (6, '本科'), (7, '硕士'),
        (8, '博士及以上')), null=True, blank=True)
    current_status = models.IntegerField(verbose_name='当前状态', choices=(
        (1, '在职看机会'), (2, '离职'), (3, '在校生等'), (4, '兼职')), null=True,
                                         blank=True)  # 当前状态(1-在职看机会,2-离职,3-在校生等)
    resume_url = models.CharField(max_length=500, verbose_name='简历URL', default='暂知', null=True, blank=True)
    aveter = models.CharField(max_length=500, verbose_name='头像', default='暂知', null=True, blank=True)
    user = models.ForeignKey(User, verbose_name='用户id', on_delete=models.CASCADE, null=True, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "jobinfo"


# 个人经历表
class WorkExperience(models.Model):
    job_info = models.ForeignKey(JobInfo, verbose_name='个人信息', on_delete=models.CASCADE,
                                 related_name='work_experiences')
    company = models.CharField(max_length=100, verbose_name='公司名称', null=True, blank=True)
    position = models.CharField(max_length=50, verbose_name='职位名称', null=True, blank=True)
    start_date = models.DateField(verbose_name='开始日期', null=True, blank=True)
    end_date = models.DateField(verbose_name='结束日期', null=True, blank=True, help_text='空值表示至今')
    description = models.TextField(verbose_name='工作描述', null=True, blank=True)
    salary_range = models.CharField(max_length=50, verbose_name='薪资范围', null=True, blank=True)
    industry = models.IntegerField(verbose_name='行业', choices=(
        (1, '互联网'), (2, '金融'), (3, '教育'), (4, '医疗健康'), (5, '房地产'),
        (6, '制造业'), (7, '消费品'), (8, '物流运输'), (9, '文化传媒'), (10, '其他')
    ), null=True, blank=True)
    is_current = models.BooleanField(verbose_name='是否当前工作', default=False)

    class Meta:
        verbose_name = 'workexperience'
        verbose_name_plural = '工作经历'
        ordering = ['-start_date']  # 按开始日期倒序排列

    def __str__(self):
        return f"{self.company} - {self.position}"

    def duration(self):
        """计算工作时长"""
        if self.start_date and self.end_date:
            delta = self.end_date - self.start_date
            years = delta.days // 365
            months = (delta.days % 365) // 30
            if years > 0:
                return f"{years}年{months}个月" if months > 0 else f"{years}年"
            else:
                return f"{months}个月"
        return "未指定"


# 求职者浏览的公司
class BrowseCompany(models.Model):
    jobinfo = models.ForeignKey(JobInfo, verbose_name='用户id', on_delete=models.CASCADE)
    company = models.ForeignKey(Company, verbose_name='公司id', on_delete=models.CASCADE)

    class Meta:
        db_table = "browseCompany"


# 求职者收藏公司
class CollectCompany(models.Model):
    jobinfo = models.ForeignKey(JobInfo, verbose_name='用户id', on_delete=models.CASCADE)
    company = models.ForeignKey(Company, verbose_name='公司id', on_delete=models.CASCADE)

    class Meta:
        db_table = "collectCompany"


# 第三方登录记录表
class ThirdLogin(models.Model):
    userid = models.ForeignKey(User, verbose_name='用户id', on_delete=models.CASCADE)
    oauth_id = models.CharField(max_length=100, verbose_name='第三方登录id', unique=True)
    style = models.CharField(max_length=20, verbose_name='第三方登录类型')
    token = models.CharField(max_length=500, verbose_name='第三方登录token')

    class Meta:
        db_table = "thirdLogin"


# 实名认证表
class RealNameVerification(models.Model):
    VERIFICATION_STATUS_CHOICES = (
        (0, '待审核'),
        (1, '审核通过'),
        (2, '审核拒绝'),
    )

    user = models.ForeignKey(User, verbose_name='用户id', on_delete=models.CASCADE)
    real_name = models.CharField(max_length=50, verbose_name='真实姓名', null=True, blank=True)
    id_card_number = models.CharField(max_length=18, verbose_name='身份证号码', unique=True, null=True, blank=True)
    id_card_front = models.CharField(max_length=500, verbose_name='身份证正面照片URL')
    id_card_back = models.CharField(max_length=500, verbose_name='身份证背面照片URL')
    verification_status = models.IntegerField(verbose_name='认证状态', choices=VERIFICATION_STATUS_CHOICES, default=0)
    reject_reason = models.TextField(verbose_name='拒绝原因', blank=True, null=True)
    verified_at = models.DateTimeField(verbose_name='认证通过时间', blank=True, null=True)
    created_at = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    updated_at = models.DateTimeField(verbose_name='更新时间', auto_now=True)

    def __str__(self):
        return f"{self.user.phone} - {self.real_name or '未填写姓名'}"

    class Meta:
        db_table = "real_name_verification"
        verbose_name = "实名认证"
        verbose_name_plural = "实名认证"


class AdminUser(models.Model):
    phone = models.CharField(max_length=11, verbose_name="手机号", null=True, blank=True)
    username = models.CharField(max_length=100, verbose_name="用户名", null=True, blank=True)
    openid = models.CharField(max_length=100, verbose_name="QQ OpenID", null=True, blank=True, unique=True)
    avatar = models.URLField(max_length=500, verbose_name="头像", null=True, blank=True)
    role = models.ForeignKey("Role", verbose_name="角色id", on_delete=models.CASCADE, null=True, blank=True)

    class Meta:
        verbose_name = "用户表"
        verbose_name_plural = "用户表"


class ResourceInterface(models.Model):
    resid = models.ForeignKey("Resource", verbose_name="资源id", on_delete=models.SET_NULL, null=True)
    url = models.CharField(max_length=100, verbose_name="接口地址")

    class Meta:
        verbose_name = "接口表"
        verbose_name_plural = "接口表"


# 七牛云文件上传记录表
class UserFile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户id')
    file_url = models.CharField(max_length=500, verbose_name='文件URL')
    file_name = models.CharField(max_length=255, verbose_name='原始文件名')
    file_type = models.CharField(
        max_length=20,
        choices=[('idcard', '身份证'), ('resume', '简历')],
        verbose_name='文件类型'
    )
    upload_time = models.DateTimeField(auto_now_add=True, verbose_name='上传时间')

    class Meta:
        db_table = "userFile"
        verbose_name = '用户上传文件'

    def __str__(self):
        return f"{self.user.phone}-{self.file_name}"
